package org.wzl.service.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.wzl.service.convert.UserMapperStruct;
import org.wzl.service.vo.UserInfoVO;
import org.wzl.service._do.UserInfo;
import org.wzl.service._do.UserLogin;
import org.wzl.service.bo.UserDetail;
import org.wzl.service.bo.redis.RedisUserBO;
import org.wzl.service.mapper.UserLoginMapper;
import org.wzl.service.service.UserInfoService;
import org.wzl.service.mapper.UserInfoMapper;
import org.springframework.stereotype.Service;

/**
* @author 卫志龙
* @description 针对表【t_user_info】的数据库操作Service实现
* @createDate 2024-06-29 16:50:10
*/
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo>
    implements UserInfoService{

    @Resource
    private UserLoginMapper userLoginMapper;

    @Resource
    private UserMapperStruct userMapperStruct;

    @Override
    public UserInfoVO getUserInfo() {
        return userMapperStruct.getVO(getCurrentUser().getRedisUserBO());
    }

    @Override
    public UserDetail loadUserByUserId(String userId) {
        UserInfo userInfo = getById(userId);
        UserLogin userLogin = userLoginMapper.selectById(userId);
        return new UserDetail(new RedisUserBO(userInfo, userLogin));
    }

    @Override
    public UserDetail getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        return (UserDetail) authentication.getPrincipal();
    }

    @Override
    public String getCurrentUserId() {
        return getCurrentUser().getRedisUserBO().getUserInfo().getId();
    }
}




